package com.vshining.grame

import org.grails.databinding.BindingFormat

class User {
    String id = UUID.randomUUID().toString()
    String username
    String passwordHash
    @BindingFormat('yyyy-MM-dd HH:mm:ss')
    Date dateCreated
    @BindingFormat('yyyy-MM-dd HH:mm:ss')
    Date lastUpdated

    static hasMany = [roles: Role, permissions: String]

    static constraints = {
        //字符、数字或下划线开头，点号只能分隔字符或数字，其他内容可由字符、数字、下划线、减号组成，但必须以字符、数字或下划线结束。
        id bindable: true, matches: /^[\w\d_]+(?:(?:[\w\d].[\w\d])?[\w\d_-]+[\w\d_])*/
        // \/:*?"<>|以及控制字符不能作为名称
        username nullable: false, blank: false, matches: /[^\\\/:*?"<>|\x00-\x1F\x7F]+/
    }

    static mapping = {
        id generator: 'assigned'
    }

    /* 用户默认具有Anyone角色 */

    def beforeInsert() {
        addToRoles(Role.findByName(SystemRoleName.Anyone.name()));
    }

}
